<template>
  <div class="workshop">
    <div>
      <label>{{ $t("route.Basic_Management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Workshop_list") }}</label>
      </button>
    </div>

    <div>
      <div>
        <div class="div-handle-right">
          <el-button>{{ $t("route.Delete") }}</el-button>
          <el-button @click="jumpPage(0)">{{ $t("route.Add") }}</el-button>
        </div>
      </div>

      <el-table class="table" :data="workshopList" style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" :label="$t('route.designation')" />
        <!-- <el-table-column prop="uhf" label="UHF" /> -->
        <!-- <el-table-column prop="gateway" label="网关" /> -->
        <el-table-column prop="factory" label="所属工厂" />
        <el-table-column prop="remark" :label="$t('route.Remarks')" />
        <el-table-column :label="$t('route.operate')">
          <template #default="scope">
            <!-- <el-button @click='uhfClick(scope.row.id)' type="text">修改UHFID</el-button> -->
            <!-- <el-button @click='gatewayClick(scope.row.id)' type="text">修改网关ID</el-button> -->
            <el-button
              @click="deleteClick(scope.row.id)"
              style="color: #ff0000"
              type="text"
              >{{ $t("route.Delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import { ElMessageBox } from "element-plus";
import i18n from "../../i18n";
import Constant from "../../js/common/constant.js";

export default {
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        search: null,
      },
      workshopList: [],
    };
  },
  methods: {
    getWorkshopList() {
      http.post(
        api.GET_WORKSHOP_LIST,
        {
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
          adminId: this.$store.state.user.id,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
        },
        (data) => {
          this.workshopList = data.data.workshopList;
          this.query.total = data.data.total;
        }
      );
    },
    jumpPage(type) {
      if (type == 0) {
        this.$router.push("/add-workshop");
      }
    },
    deleteClick(workshopId) {
      this.$confirm(i18n.global.t("route.continue22"), i18n.global.t("route.reminder"), {
        confirmButtonText: i18n.global.t("route.sure"),
        cancelButtonText: i18n.global.t("route.cancel"),
        type: "warning",
      }).then(() => {
        http.post(
          api.DELETE_WORKSHOP,
          {
            workshopId: workshopId,
          },
          (data) => {
            this.$message.success(i18n.global.t("route.Delete_succeeded"));
            this.getWorkshopList();
          }
        );
      });
    },
    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getWorkshopList();
    },
    uhfClick(workshopId) {
      ElMessageBox.prompt(
        i18n.global.t("route.between"),
        i18n.global.t("route.Modify_UHFID"),
        {
          confirmButtonText: i18n.global.t("route.sure"),
          cancelButtonText: i18n.global.t("route.cancel"),
          inputType: "number",
        }
      ).then(({ value }) => {
        if (value < 1 || value > 255) {
          this.$message.error(i18n.global.t("route.The_number_is_between_1_and_255"));
        } else {
          this.updateWorkshopUhf(workshopId, value);
        }
      });
    },
    gatewayClick(workshopId) {
      ElMessageBox.prompt(
        i18n.global.t("route.between"),
        i18n.global.t("route.Modify_UHFID"),
        {
          confirmButtonText: i18n.global.t("route.sure"),
          cancelButtonText: i18n.global.t("route.cancel"),
          inputType: "number",
        }
      ).then(({ value }) => {
        if (value < 1 || value > 255) {
          this.$message.error(i18n.global.t("route.The_number_is_between_1_and_255"));
        } else {
          this.updateWorkshopGateway(workshopId, value);
        }
      });
    },
    updateWorkshopUhf(workshopId, uhf) {
      http.post(
        api.UPDATE_WORKSHOP_UHF,
        {
          workshopId: workshopId,
          uhf: uhf,
        },
        (data) => {
          this.$message.success(i18n.global.t("route.Modified_successfully"));
          this.getWorkshopList();
        }
      );
    },
    updateWorkshopGateway(workshopId, gateway) {
      http.post(
        api.UPDATE_WORKSHOP_GATEWAY,
        {
          workshopId: workshopId,
          gateway: gateway,
        },
        (data) => {
          this.$message.success(i18n.global.t("route.Modified_successfully"));
          this.getWorkshopList();
        }
      );
    },
  },
  created() {
    this.getWorkshopList();
  },
};
</script>
<style lang="scss" scoped>
.workshop {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div {
    display: flex;
    flex-direction: row;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;
  }

  > div:nth-of-type(1) {
    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    flex-direction: column;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }

    .table {
      margin-top: 20px;
    }
  }
}
</style>
